<?php
/**
 * Created by PhpStorm.
 * User: ysongyang
 * Date: 2016/6/8
 * Time: 17:56
 */
namespace Admin\Controller;

use Admin\Common\Libs\MySQLReback;
class SqlBackController extends CommonController{


    //数据备份
    public function index(){
        $DataDir = "Databak";
        if(!file_exists($DataDir))mkdir($DataDir) ;
        $opt = trim(I('get.opt'));
        $file = trim(I('get.file')).".sql";
        $mr = new MySQLReback();
        $mr->setDBName(C('DB_NAME'));
        $mr->setPath($DataDir);
        if ($opt) {
            if ($opt == 'backup') {
                $mr->backup()?$alert = '数据库备份成功！':$alert = $mr->error();
            }
            if($opt == 'optimize'){
                $mr->optimize()?$alert = '数据库优化成功！':$alert = $mr->error();
            }
            if ($opt == 'redo' && $file) {
                $r = $mr->recover($file);
                $alert = '数据库还原成功'.$r['qty'] ."条";
            }
            if ($opt== 'del' && $file) {
                $mr->remove($file)?$alert = '数据库删除成功！':$alert = $mr->error();
            }
            if ($opt == 'download' && $file ) $mr->DownloadFile($file);
        }
        $lists = $mr->dataList($DataDir);
        $this->assign("list", $lists);
        if($alert)
            $this->assign('alert',"alert('".$alert."');");
        $this->display();
    }
}